
import getIn from '../utils/getIn';
import fetchMemberWxLogin from '../business/member/fetchMemberWxLogin';
import { notify } from './NotifyRegistryCenter';
export function login(callback, domain) {
    wx.login({
        success(res) {
            const code = res && res.code;
            wx.getUserInfo({
                success: (res) => {
                    const { iv } = res;
                    const { encryptedData } = res;
                    const params = domain ? {
                      domain, code, encryptedData, iv,
                    } : {
                        code, encryptedData, iv,
                      };
                    wx.showLoading({ title: '正在登录' });
                    global.ActionDispatcher.execute(fetchMemberWxLogin, params, (status, result) => {
                        if (status == 'SUCCESS') {
                            wx.hideLoading();
                            wx.setStorageSync('token', getIn(result, ['data', 'token']));
                            wx.setStorageSync('openid', getIn(result, ['data', 'openid']));
                            getApp().globalData.token = getIn(result, ['data', 'token']);
                            getApp().globalData.openid = getIn(result, ['data', 'openid']);
                            notify('LOGIN_NOTI', getIn(result, ['data', 'token']));
                            callback && callback('SUCCESS', result);
                        } else if (status == 'FAIL') {
                            wx.hideLoading();
                            callback && callback('FAIL', result);
                            const title = (result && result.msg) || '登录失败';
                            const time = 2000;
                            const image = '/image/ico_warning.png';
                            wx.showToast({
                                title,
                                image,
                                time,
                            });
                        }
                    });
                },


            });
        },
    });
}
export function checkLogin(callback) {
    wx.getSetting({
        success: (res) => {
            if (res.authSetting['scope.userInfo'] == true) {
                wx.showModal({
                    title: '提示',
                    content: '该页面需要您登录才能使用，请点击重新登录。',
                    confirmText: '重新登录',
                    success(res) {
                        if (res.confirm) {
                            // 代表已经授权过 直接登录
                            login((status, result) => {
                                if (status == 'SUCCESS') {
                                    notify('updateToken');
                                }
                            });
                        }
                    },
                });
            } else {
                // 调出授权再登录
                wx.showModal({
                    title: '提示',
                    content: '该页面需要获取您的昵称等信息，请到小程序设置界面打开用户授权功能！',
                    confirmText: '重新授权',
                    success(res) {
                        if (res.confirm) {
                            wx.openSetting({
                                success: (res) => {
                                    if (res.authSetting['scope.userInfo'] == true) {
                                        login((status, result) => {
                                            if (status == 'SUCCESS') {
                                                notify('updateToken');
                                            }
                                        });
                                    }
                                },
                            });
                        }
                    },
                });
            }
        },
    });
}

